const express = require("express");
const cors = require("cors");
const { goods_router } = require("./router/goodsRouter");
const { orderRouter } = require("./router/orderRouter");
const { userRouter } = require("./router/userRouter");
const { addressRouter } = require("./router/addressRouter");
const { commentRouter } = require("./router/CommentRouter");
const { swiperRouter } = require("./router/swiperRouter");
const { storeRouter } = require("./router/storeRouter");



const app = express();

app.use(cors());

/* 配置中间件 */
app.use(express.json());
app.use(express.urlencoded({extended:true}));

/* 处理错误的中间件 */
app.use((req,res,next)=>{
    res.fail = function(err,status = 1){
        res.send({
            status,
            message:err instanceof Error ? err.message :err
        })
    }
    next();
})

/* 关于商品路由 */
app.use('/api/goods', goods_router);
/* 关于订单路由 */
app.use("/api/order",orderRouter);
/* 关于用户路由 */
app.use('/api/user',userRouter);
/* 关于地址路由 */
app.use('/api/address',addressRouter);
/* 关于评论路由 */
app.use("/api/comment",commentRouter);
/* 关于轮播图路由 */
app.use("/api/swiper",swiperRouter);
/* 关于店铺路由 */
app.use("/api/store",storeRouter);

app.listen(3000, () => {
    console.log("server is running at http://127.0.0.1:3000");
})